Search a literals string in a string and pattern location¶
Search a literals string in a string and also
find the location within the original string where the pattern occurs.
Sample text : ‘The quick brown fox jumps over the lazy dog.’
Searched words : ‘fox’
import re
pattern = 'fox'
S = 'The quick brown fox jumps over the lazy dog.'
match = re.search(pattern, S)
s = match.start()
e = match.end()
print('Found "%s" in "%s" from %d to %d ' % \
(match.re.pattern, match.string, s, e))
Output:
Found "fox" in "The quick brown fox jumps over the lazy dog." from 16 to 19